<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="./js/vue.js"></script>
</head>

<body>
  <div id="app">
    <div>
      <input type="text" :value="title" @input="inputFn" @keyup="onenter">
    </div>
    <div>
      <ul>
        <li v-for="(item,index) in todos" :key="item.id">
          <span>{{item.title}}</span> --
          <!-- <span @click="delTodo(item.id)">删除</span> -->
          <span @click="delTodo(index)">删除</span>
        </li>
      </ul>
    </div>

  </div>

  <script>

    new Vue({
      el: '#app',
      data: {
        title: '',
        todos: []
      },
      methods: {
        inputFn(evt) {
          this.title = evt.target.value.trim()
        },
        onenter(evt) {
          if (evt.key === 'Enter') {
            // push它是vue重构一个数组对象的方法，调用它会触发视图更新
            this.todos.push({
              id: Date.now(),
              title: this.title
            })
            this.title = ''
          }
        },
        // delTodo(id) {
        delTodo(index) {
          // this.todos = this.todos.filter(item => item.id != id)
          /* let index = this.todos.findIndex(item => item.id == id)
          if (index >= 0) {
            // vue对于此数组方法也进行重构，调用它也是引发视图更换
            this.todos.splice(index,1)
          } */
          this.todos.splice(index, 1)
        }
      }
    })
  </script>
</body>

</html>